package com.lei.linearsearch;

/**
 * @Author LeiXiaoLei
 * @Date 2022/7/14 15:43
 * @Version 1.0
 * <p>
 * 输入： 数组，和目标元素
 * 输出： 目标元素所在的索引；若不存在，返回-1
 */
public class LinearSearch {

    private LinearSearch() {
    }

    public static <E> int search(E[] data, E target) {
        for (int i = 0; i < data.length; i++) {
            if (data[i].equals(target)) return i;
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] dataSize = {1000000, 10000000};
        for (int n : dataSize) {
            Integer[] data = ArrayGenerator.generatorOrderedArray(n);
            long startTime = System.nanoTime();
            for (int i = 0; i < 100; i++) {
                LinearSearch.search(data, n);
            }
            long endTime = System.nanoTime();
            double time = (endTime - startTime) / 1000000000.0;
            System.out.println("n = " + n + ", 100 runs: " + time + " s");
        }
    }
}
